<template>
  <div>
    <Content style="text-align:right">
      <Form :label-width="120">
        <FormItem label="商品名称">
          <Checkbox disabled :value='true'>{{dataBus.skuName}}</Checkbox>
        </FormItem>
        <FormItem label="活动状态">
          <Checkbox disabled :value='true'>{{activeStatusText}}</Checkbox>
        </FormItem>
        <FormItem label="定时任务">
          <Checkbox disabled :value='true'>{{taskStatusBool?'开启':'关闭'}}</Checkbox>
        </FormItem>
        <FormItem label="活动开始时间">
          <DatePicker type="datetime" v-model="startTimeFormat" @on-change="datePickerStart" placeholder="选择开始时间" style="width: 320px"></DatePicker>
        </FormItem>
        <FormItem label="活动结束时间">
          <DatePicker type="datetime" v-model="stopTimeFormat" @on-change="datePickerStop" placeholder="选择结束时间" style="width: 320px"></DatePicker>
        </FormItem>
        <!-- <FormItem label="定时任务状态">
          <Switch size="large" :disabled="taskStatusDisable" v-model="taskStatusBool" @on-change="OpenChange">
            <span slot="open">开启</span>
            <span slot="close">关闭</span>
          </Switch>
        </FormItem> -->
        <FormItem>
					<Button type="primary" :disabled="taskStatusBool || taskStatusDisable" @click="Action">开启</Button>
					<Button :disabled="!taskStatusBool" @click="Cancel">关闭</Button>
				</FormItem>
      </Form>
    </Content>
  </div>
</template>
<script>
export default {
  name: 'ReleaseModule',
  props: {
    callbank: Function,
    dataBus: Object
  },
  data() {
    return {
      startTimeFormat: null,
      stopTimeFormat: null,
      activeStatusText: null,
      taskStatusBool: false,
      taskStatusDisable: true,
      flashSale: {}
    }
  },
  methods: {
    //初始化
    Init(skuId) {
      this.getShopGoods(skuId)
    },
    getShopGoods(skuId) {
      new this.$rest({
        gateway: 'agroerp',
				service: 'agroerp-mall',
				url: '/v1.0/agromall/shop/goods/skuid',
        query: {
          equal: skuId
        },
				action: (res)=>{
					if(res.data.code === 20000 && Object.keys(res.data.data).length > 0) {
						if(res.data.data.flashSale) {
              this.flashSale = res.data.data.flashSale
              if(this.flashSale.startTime && this.flashSale.stopTime) {
                this.taskStatusDisable = false
              }
              if(this.flashSale.startTime) {
                this.startTimeFormat = this.$date(this.flashSale.startTime,'yyyy-MM-dd HH:mm:ss')
              }
              if(this.flashSale.stopTime) {
                this.stopTimeFormat = this.$date(this.flashSale.stopTime,'yyyy-MM-dd HH:mm:ss')
              }
              this.activeStatusText = this.praseStatus(this.flashSale.activeStatus)
              this.taskStatusBool = this.flashSale.taskStatus === 1
            } else {
              this.flashSale.shopGoodsId = res.data.data.shopGoodsId
            }
					}
				}
			}).get()
    },
    OpenChange(status) {
      if(status) {
        this.Action()
      } else {
        this.Cancel()
      }
    },
    praseStatus(status) {
      switch(status) {
        case 0:
          return '未开始'
        case 1:
          return '进行中'
        case 2:
          return '已结束'
        default:
          return '未知'
      }
    },
    datePickerStart(e) {
      this.flashSale.startTime = new Date(e).getTime()
    },
    datePickerStop(e) {
      this.flashSale.stopTime = new Date(e).getTime()
    },
    //激活任务
    Action() {
      if(!this.flashSale.startTime) {
        this.$Message.warning({
          content: '请选择开始时间'
        })
        this.taskStatusBool = false
        return false
      }
      if(!this.flashSale.stopTime) {
        this.$Message.warning({
          content: '请选择结束时间'
        })
        this.taskStatusBool = false
        return false
      }

      new this.$rest({
        gateway: 'agroerp',
        service: "agroerp-mall",
        url: "/v1.0/agromall/active/flash/sale/task/status",
        data: {
          pojo: this.flashSale
        },
        action: (res) => {
          if (res.data.code === 20000) {
            this.$Message.success({
              content: '开启成功',
              onClose: ()=>{
                this.callbank()
              }
            })
          } else {
            this.$Notice.warning({
							title: "操作失败",
							desc: `失败原因：${res.data.msg}`
						})
          }
        },
      }).post()
    },
    //撤销任务
    Cancel() {
      new this.$rest({
        gateway: 'agroerp',
        service: "agroerp-mall",
        url: "/v1.0/agromall/cancel/flash/sale/task/status",
        data: {
          pojo: this.flashSale.shopGoodsId
        },
        action: (res) => {
          if (res.data.code === 20000) {
            this.$Message.success({
              content: '关闭成功',
              onClose: ()=>{
                this.callbank()
              }
            })
          } else {
            this.$Notice.warning({
							title: "操作失败",
							desc: `失败原因：${res.data.msg}`
						})
          }
        },
      }).post()
    }
  },
  watch: {
    startTimeFormat(val) {
      if(this.flashSale.stopTime && val) {
        this.taskStatusDisable = false
      } else {
        this.taskStatusDisable = true
      }
    },
    stopTimeFormat(val) {
      if(this.flashSale.startTime && val) {
        this.taskStatusDisable = false
      } else {
        this.taskStatusDisable = true
      }
    }
  },
  mounted() {
    
  },
  created() {
    
  },
};
</script>

<style lang="less" scoped>
.cust-form {
  text-align: left !important;
}
</style>
